package com.pearadmin.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.hospital.domain.HospitalSupplier;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.hospital.service.IHospitalSupplierService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 供应商Controller
 *
 * @author yuan
 * @date 2021-07-29
 */
@RestController
@RequestMapping("/hospital/supplier")
public class HospitalSupplierController extends BaseController
{
    private String prefix = "hospital/supplier";

    @Autowired
    private IHospitalSupplierService hospitalSupplierService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/hospital/supplier/main','hospital:supplier:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询供应商列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/hospital/supplier/data','hospital:supplier:data')")
    public ResultTable list(@ModelAttribute HospitalSupplier hospitalSupplier, PageDomain pageDomain)
    {
        PageInfo<HospitalSupplier> pageInfo = hospitalSupplierService.selectHospitalSupplierPage(hospitalSupplier,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增供应商
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/hospital/supplier/add','hospital:supplier:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存供应商
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/hospital/supplier/add','hospital:supplier:add')")
    public Result save(@RequestBody HospitalSupplier hospitalSupplier)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalSupplier.setCreateTime(LocalDateTime.now());
        hospitalSupplier.setCreateBy(sysUser.getUserId());
        hospitalSupplier.setCreateName(sysUser.getUsername());
        return decide(hospitalSupplierService.insertHospitalSupplier(hospitalSupplier));
    }

    /**
     * 修改供应商
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/hospital/supplier/edit','hospital:supplier:edit')")
    public ModelAndView edit(Integer id, ModelMap mmap)
    {
        HospitalSupplier hospitalSupplier = hospitalSupplierService.selectHospitalSupplierById(id);
        mmap.put("hospitalSupplier", hospitalSupplier);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存供应商
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/hospital/supplier/edit','hospital:supplier:edit')")
    public Result update(@RequestBody HospitalSupplier hospitalSupplier)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalSupplier.setUpdateTime(LocalDateTime.now());
        hospitalSupplier.setUpdateBy(sysUser.getUserId());
        hospitalSupplier.setUpdateName(sysUser.getUsername());
        return decide(hospitalSupplierService.updateHospitalSupplier(hospitalSupplier));
    }

    /**
     * 删除供应商
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/hospital/supplier/remove','hospital:supplier:remove')")
    public Result batchRemove(String ids)
    {
        return decide(hospitalSupplierService.deleteHospitalSupplierByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/hospital/supplier/remove','hospital:supplier:remove')")
    public Result remove(@PathVariable("id") Integer id)
    {
        return decide(hospitalSupplierService.deleteHospitalSupplierById(id));
    }
    /**
     * 获取供应商
     */
    @ResponseBody
    @GetMapping("/getSupplierList")
    public Result getSupplierList(){
        List<HospitalSupplier> supplierList = hospitalSupplierService.selectHospitalSupplierList(null);
        return Result.success(supplierList);
    }
    /**
     * 根据id获取供应商
     */
    @GetMapping("/getSupplierById")
    public Result getSupplierById(Integer id){
        if (null ==id ) {
            return failure();
        }
        HospitalSupplier hospitalSupplier = hospitalSupplierService.selectHospitalSupplierById(id);
        return Result.success(hospitalSupplier);
    }
}
